#include "app_buffer.h"
#include "log/log.h"
#include <stdlib.h>
#include <string.h>

// 定义一个初始化锁
static pthread_mutex_t init_lock = PTHREAD_MUTEX_INITIALIZER;

Buffer *app_buffer_init(int size)
{
    // 检查参数的合法性
    if (size <= 0)
    {
        log_error("buffer size must be greater than 0");
        return NULL;
    }

    // 分配Buffer的内存
    Buffer *buffer = malloc(sizeof(Buffer));
    if (buffer == NULL)
    {
        log_error("malloc buffer failed");
        return NULL;
    }
    // 初始buffer的data属性
    buffer->data = malloc(size);
    if (!buffer->data)
    {
        log_error("malloc buffer data failed");
        free(buffer);
        return NULL;
    }
    // 初始化buffer的其它属性
    buffer->total_size = size;
    buffer->len = 0;
    buffer->offset = 0;

    // 初始化buffer的lock属性
    buffer->lock = init_lock;

    return buffer;
}


void app_buffer_free(Buffer *buffer)
{
    // 检查参数的合法性
    if (buffer == NULL)
    {
        log_error("buffer is NULL");
        return;
    }
    // 释放buffer的data属性
    if (buffer->data)
    {
        free(buffer->data);
    }
    // 释放buffer的内存
    free(buffer);
}


int app_buffer_write(Buffer *buffer, void *data, int len)
{
    // 检查参数的合法性
    if (buffer == NULL || data == NULL || len <= 0)
    {
        log_error("invalid parameters");
        return -1;
    }

    // 加锁
    pthread_mutex_lock(&buffer->lock);

    // 检查buffer是否还有足够的空间
    if (buffer->total_size - buffer->len < len)
    {
        log_error("buffer not enough space");
        // 解锁
        pthread_mutex_unlock(&buffer->lock);
        return -1;
    }

    // 计算写入的位置
    int write_start = buffer->offset + buffer->len;
    // 修正write_start
    if (write_start >= buffer->total_size)
    {
        write_start = write_start - buffer->total_size;
    }
    // 写入数据
    if (write_start + len <= buffer->total_size) // 写一段
    {
       memcpy(buffer->data + write_start, data, len);
    }
    else // 写两段
    {
        // 计算第一段长度
        int len1 = buffer->total_size - write_start;
        // 写入第一段
        memcpy(buffer->data + write_start, data, len1);
        // 写入第二段
        memcpy(buffer->data, data+len1, len-len1);
    }
    // 更新buffer的len属性
    buffer->len += len;

    // 解锁
    pthread_mutex_unlock(&buffer->lock);

    log_debug("write success offset: %d, len: %d, data: %s", 
        buffer->offset, buffer->len, buffer->data);
    return 0;
}


int app_buffer_read(Buffer *buffer, void *data, int len)
{
    // 检查参数的合法性
    if (buffer == NULL || data == NULL || len <= 0)
    {
        log_error("invalid parameters");
        return -1;
    }

    // 加锁
    pthread_mutex_lock(&buffer->lock);

    // 检查buffer中是否有足够的数据
    if (buffer->len < len)
    {
        log_error("buffer not enough data");
        // 解锁
        pthread_mutex_unlock(&buffer->lock);
        return -1;
    }

    // 从缓存中读取数据
    if (buffer->offset + len <= buffer->total_size) // 读一段
    {
        memcpy(data, buffer->data+buffer->offset, len);
        // 更新buffer的offset属性
        buffer->offset += len;
    }
    else // 读两段
    {
        // 计算第一段长度
        int len1 = buffer->total_size - buffer->offset;
        // 读取第一段
        memcpy(data, buffer->data+buffer->offset, len1);
        // 读取第二段
        memcpy(data + len1, buffer->data, len - len1);
        // 更新buffer的offset属性
        buffer->offset = len - len1;
    }
    // 更新buffer的len属性
    buffer->len -= len;

    // 解锁
    pthread_mutex_unlock(&buffer->lock);
    
    log_debug("read success offset: %d, len: %d, data: %s", 
        buffer->offset, buffer->len, buffer->data);

    return 0;
}